package com.example.check_oa.model.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.check_oa.model.mapper.*;
import com.example.check_oa.model.service.ReplacementCardApService;
import com.example.check_oa.utils.GetAntten;
import com.example.comment_oa.pojos.attendance.*;
import com.example.comment_oa.pojos.MouthAt;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
* @author cjl
* @description 针对表【replacement_card_ap】的数据库操作Service实现
* @createDate 2022-11-09 21:46:25
*/
@Service
public class ReplacementCardApServiceImpl extends ServiceImpl<ReplacementCardApMapper, ReplacementCardAp>
    implements ReplacementCardApService{
    @Resource
    ReplacementCardApMapper cardApMapper;
    @Resource
    AttendanceMapper attendanceMapper;
    @Resource
    OvertimeApMapper overtimeApMapper;
    @Resource
    BusinessTripApMapper businessTripApMapper;
    @Resource
    HolidayApMapper holidayApMapper;
    @Resource
    MouthAtMapper mouthAtMapper;
    private String url = "D:\\study\\gitee\\my_springcloud_project01cangku\\capacity\\src\\views\\capacity\\cjl\\img";


    //add补卡
    @Override
    public int add(ReplacementCardAp replacementCardAp, MultipartFile file) {
        //上月已归档
        QueryWrapper<MouthAt> wrapper8=new QueryWrapper<>();
        Date rcTime = replacementCardAp.getRcTime();
        wrapper8.like("mouth",DateUtil.formatDate(rcTime).substring(0,7));
        List<MouthAt> mouthAts = mouthAtMapper.selectList(wrapper8);
        if(!mouthAts.isEmpty()){
            return 6;
        }
        QueryWrapper<ReplacementCardAp> wrapper1=new QueryWrapper<>();
        wrapper1.eq("user_rc",replacementCardAp.getUserRc());
        wrapper1.like("rc_time",DateUtil.formatDate(replacementCardAp.getRcTime()));
        wrapper1.eq("rc_attendance_type_id",replacementCardAp.getRcAttendanceTypeId());
        ReplacementCardAp replacementCardAp1 = cardApMapper.selectOne(wrapper1);
        if(replacementCardAp1!=null){
            return 2;
        }
        QueryWrapper<ReplacementCardAp> wrapper=new QueryWrapper<>();
        wrapper.eq("user_rc",replacementCardAp.getUserRc());
        wrapper.like("rc_time",DateUtil.formatDate(replacementCardAp.getRcApTime()).substring(0,7));
        wrapper.eq("rc_state",1);
        List<ReplacementCardAp> replacementCardAps = cardApMapper.selectList(wrapper);
        if(replacementCardAps.size()>=3){
            return 1;
        }
        if(file!=null){
            //获得文件的名称
            String fileName = file.getOriginalFilename();
            fileName = file.getOriginalFilename();
            //取后缀名
            String ext = FilenameUtils.getExtension(fileName);
            //创建一个新的文件名
            fileName = UUID.randomUUID().toString() +"."+ ext;
            File f =  new File(url,fileName);
            try {
                file.transferTo(f);//进行文件转移
            } catch (IOException e) {
                e.printStackTrace();
            }
            String filePath = "capacity/cjl/img/"+fileName;
            replacementCardAp.setRcFile(filePath);
        }
        int insert = cardApMapper.insert(replacementCardAp);
        if (insert!=0){
            return 3;
        }
        return 3;
    }

    //查找近三天缺卡记录
    @Override
    public List<Attendance> selectNo(String userNo) {
        //加班集合
        List<String> ii=new ArrayList<>();
        QueryWrapper<OvertimeAp> ovWrapper=new QueryWrapper<>();
        ovWrapper.eq("user_ot",userNo);
        ovWrapper.eq("ot_state",1);
        List<OvertimeAp> overtimeAps = overtimeApMapper.selectList(ovWrapper);
        overtimeAps.forEach(e-> {
            List<String> betweenDate = GetAntten.getBetweenDate(DateUtil.formatDate(e.getOtStartTime()), DateUtil.formatDate(e.getOtEndTime()));
            ii.addAll(betweenDate);
        });
        //出差集合
        QueryWrapper<BusinessTripAp> busiQueryWrapper=new QueryWrapper<>();
        busiQueryWrapper.eq("user_bt",userNo);
        busiQueryWrapper.eq("bt_ap_state",1);
        List<String> busiList=new ArrayList<>();
        List<BusinessTripAp> businessTripAps = businessTripApMapper.selectList(busiQueryWrapper);
        businessTripAps.forEach(e->{
            List<String> betweenDate = GetAntten.getBetweenDate(DateUtil.formatDate(e.getBtStartTime()), DateUtil.formatDate(e.getBtEndTime()));
            busiList.addAll(betweenDate);
        });
        //本月调休集合
        List<String> stringList = GetAntten.get(DateUtil.formatDate(new Date()).substring(0,7));
        //工作日+调休
        List<String> stringList1 = GetAntten.queryMonthDay(DateUtil.formatDate(new Date()), stringList);
        stringList1.removeAll(ii);//工作日+调休-加班
        stringList1.removeAll(busiList);//-出差

        //请假
        QueryWrapper<HolidayAp> holiWrapper=new QueryWrapper<>();
        holiWrapper.eq("user_ho",userNo);
        holiWrapper.eq("ho_state",1);
        List<HolidayAp> holidayAps = holidayApMapper.selectList(holiWrapper);
        List<String> betweenList=new ArrayList<>();
        holidayAps.forEach(e->{
            betweenList.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getHoApStartTime()), DateUtil.formatDate(e.getHoApEndTime())));
        });
        stringList1.removeAll(betweenList);//去除请假日，请假日不补卡
        QueryWrapper<Attendance> wrapper7=new QueryWrapper<>();
        wrapper7.like("at_time",DateUtil.formatDate(new Date()).substring(0,7));
        wrapper7.eq("user_at",userNo);
        List<String> stringList2=new ArrayList<>();
        List<Attendance> attendances = attendanceMapper.selectList(wrapper7);
        for (int a = 0; a < attendances.size(); a++) {
            stringList2.add(DateUtil.formatDate(attendances.get(a).getAtTime()));
        }
        List<String> stringList3 = GetAntten.subList2(stringList1,stringList2);
        //获取最近三天集合
        List list = GetAntten.threeDay(3);
        //最近三天和这个月旷工的有无交集
        List<String> list1 = GetAntten.intersectList2(stringList3, list);
        List<Attendance> list3=new ArrayList<>();
        list1.forEach(e->{
            Attendance attendance=new Attendance();
            attendance.setAtTime(DateUtil.parse(e));
            attendance.setTypeName("上班卡");
            list3.add(attendance);
        });
        list1.forEach(e->{
            Attendance attendance=new Attendance();
            attendance.setAtTime(DateUtil.parse(e));
            attendance.setTypeName("下班卡");
            list3.add(attendance);
        });
        //缺卡的
        List<Attendance> list2 = attendanceMapper.treeDayNo(userNo);
        System.out.println(list2);
        list2.forEach(e->{
            if(e.getAtEndRes()==null){
                e.setTypeName("下班卡");
            }else{
                e.setTypeName("上班卡");
            }
        });
        list2.addAll(list3);//加上缺卡的
        //查出这个月已经补过卡的记录
        QueryWrapper<ReplacementCardAp> wrapper=new QueryWrapper<>();
        wrapper.eq("user_rc",userNo);
        wrapper.like("rc_time",DateUtil.formatDate(new Date()).substring(0,7));
        wrapper.eq("rc_state",1);
        List<ReplacementCardAp> replacementCardAps = cardApMapper.selectList(wrapper);
        List<Attendance> list4=new ArrayList<>();
        replacementCardAps.forEach(e->{
            list2.forEach(e1->{
                if(DateUtil.formatDate(e.getRcTime()).equals(DateUtil.formatDate(e1.getAtTime()))
                && e.getRcAttendanceTypeId().equals(e1.getTypeName())){
                    list4.add(e1);
                }
            });
        });
        list2.removeAll(list4);//已经补过卡的，去除
        list2.forEach(e-> System.out.println(e));
        ArrayList<Attendance> distinct = CollUtil.distinct(list2);//去重
        return distinct;
    }

    @Override
    public List<ReplacementCardAp> quer(String userNo, String date) {
        QueryWrapper<ReplacementCardAp> wrapper=new QueryWrapper<>();
        wrapper.eq("user_rc",userNo);
        wrapper.like("rc_time",date);
        return cardApMapper.selectList(wrapper);
    }

    @Override
    public int yesRc(String time,String userRc,String rcId, String yesUser) {
        QueryWrapper<ReplacementCardAp> wrapper=new QueryWrapper<>();
        wrapper.eq("user_rc",userRc);
        wrapper.like("rc_time",time);
        wrapper.eq("rc_state",1);
        List<ReplacementCardAp> replacementCardAps = cardApMapper.selectList(wrapper);
        if(replacementCardAps.size()>=3){
            //补卡次数用完
            return 2;
        }
        UpdateWrapper<ReplacementCardAp> wrapper1=new UpdateWrapper<>();
        wrapper1.eq("rc_water",rcId);
        ReplacementCardAp replacementCardAp=new ReplacementCardAp();
        replacementCardAp.setRcApYestime(new Date());
        replacementCardAp.setRcYesUser(yesUser);
        replacementCardAp.setRcState(1);
        return cardApMapper.update(replacementCardAp,wrapper1);
    }

    @Override
    public int noRc(String rcId, String yesUser) {
        return cardApMapper.updateNo(rcId,yesUser);
    }
}




